package com.ewzj.bigdata.mvp.ui.activity

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView

import com.ewzj.bigdata.R
import com.ewzj.bigdata.di.component.DaggerCitySelectorActivityComponent
import com.ewzj.bigdata.di.module.CitySelectorActivityModule
import com.ewzj.bigdata.mvp.contract.CitySelectorActivityContract
import com.ewzj.bigdata.mvp.presenter.CitySelectorActivityPresenter
import com.ewzj.bigdata.mvp.ui.adapter.CityAdapter
import com.ewzj.bigdata.mvp.ui.adapter.HotcityAdapter
import com.gjiazhe.wavesidebar.WaveSideBar
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_city_select.*
import kotlinx.android.synthetic.main.titile_red.*
import java.util.concurrent.TimeUnit

class CitySelectActivity : BaseActivity<CitySelectorActivityPresenter>(),CitySelectorActivityContract.View {
    override fun initLocationCity(string: String) {
        tv_location.text=string
    }

    override fun initHotCityview(adapter: HotcityAdapter) {
        val gridManager = GridLayoutManager(this, 5)
        rv_hotcity.layoutManager=gridManager as RecyclerView.LayoutManager
        rv_hotcity.adapter=adapter
    }

    override fun initCitylistview(adapter: CityAdapter) {
        val manger = LinearLayoutManager(this)
        rv_citylist.addItemDecoration(HorizontalDividerItemDecoration.Builder(this).size(1)
                .colorResId(R.color.color_divider).margin(10, 10).build())
        rv_citylist.layoutManager=manger
        rv_citylist.adapter=adapter

    }

    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_city_select
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerCitySelectorActivityComponent.builder().appComponent(appComponent)
                .citySelectorActivityModule(CitySelectorActivityModule(this))
                .build()
                .inject(this)
    }

    override fun initData(savedInstanceState: Bundle?) {
        tv_title.text="城市选择"
        initListener()
        mPresenter.init()
        mPresenter.loadAllCityData()
        mPresenter.loadHotCitydata()
    }


    override fun showLoading() {

    }

    override fun launchActivity(intent: Intent?) {

    }

    override fun hideLoading() {

    }

    override fun killMyself() {

    }

    override fun showMessage(message: String?) {

    }

    private fun initListener() {
        side_bar.setOnSelectIndexItemListener(object :WaveSideBar.OnSelectIndexItemListener{
            override fun onSelectIndexItem(index: String) {
                mPresenter.scrollToHead(index,rv_citylist)
            }
        })
        RxView.clicks(iv_back).subscribe({finish()})
        RxView.clicks(tv_location).subscribe({mPresenter.locationCityData(tv_location.text.toString())})
        RxTextView.textChangeEvents(et_content).debounce(500,TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe({ t: TextViewTextChangeEvent? -> mPresenter.searchData(t!!.text().toString()) })
    }

    companion object{
        fun startCitySelectActivity(context:Context){
            var intent=Intent(context,CitySelectActivity::class.java)
            context.startActivity(intent)
        }
    }
}
